using System;
using System.Linq;
using System.Drawing.Drawing2D;
using System.Data;
using System.Data.Odbc;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;

public partial class V2Controls_Top10Nyheder : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (INewsService service = ServiceFactory.NewsService)
            {
                IOrderedEnumerable<News> newsItems = service.GetLatest(10).OrderByDescending(t => t.Id);
                this.uiRepNewsItems.DataSource = newsItems;
                this.uiRepNewsItems.DataBind();
            }
        }
    }

    protected void uiRepNewsItems_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HyperLink link = e.Item.FindControl("uiHlItem") as HyperLink;
            Literal text = e.Item.FindControl("uiLitTitle") as Literal;
            Literal date = e.Item.FindControl("uiLitDate") as Literal;

            News item = e.Item.DataItem as News;
            if (item != null)
            {
                if (link != null)
                {
                    link.NavigateUrl = UrlUtillity.GetNewsUri(item);
                }

                if (text != null)
                {
                    if (item.Title.Length > 30)
                    {
                        text.Text = item.Title.Substring(0, 30) + "...";
                    }
                    else
                    {
                        text.Text = item.Title;
                    }

                    if (link != null)
                    {
                        link.ToolTip = UrlUtillity.GetNewsUri(item);
                    }
                }

                if (date != null)
                {
                    date.Text = item.NewsDate.ToLongDateString();
                }
            }
            else
            {
                e.Item.Visible = false;
            }
        }
    }
}
